/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controls;

import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import others.Console;
import others.Login;
import others.User;

/**
 *
 * @author Dingo
 */
@ManagedBean
@RequestScoped
public class SSRControl implements Serializable {
 
    private String log;
    private String information = "...";
    @ManagedProperty(value = "#{login}")
    private Login login;

    /** Creates a new instance of ssrControl */
    public SSRControl() {
    }

    public void start(ActionEvent actionEvent) {
        if (login.userPermited(User.PERM_ADMIN)) {
            log = Console.request("iisreset /start");
            FacesMessage msg = null;

            Pattern pattern = Pattern.compile("started");
            Matcher matcher = pattern.matcher(log);

            if (matcher.find()) {
                information = "Serwer IIS został uruchomiony pomyślnie!";
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Hura!", information);
            } else {
                information = "Błąd podczas uruchamiania serwera IIS! Spróbuj ponownie.";
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Błąd!", information);
            }
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void stop(ActionEvent actionEvent) {
        if (login.userPermited(User.PERM_ADMIN)) {
            log = Console.request("iisreset /stop");
            FacesMessage msg = null;

            Pattern pattern = Pattern.compile("stopped");
            Matcher matcher = pattern.matcher(log);

            if (matcher.find()) {
                information = "Serwer IIS został zatrzymany pomyślnie!";
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Hura!", information);
            } else {
                information = "Błąd podczas zatrzymywania serwera IIS! Spróbuj ponownie.";
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Błąd!", information);
            }
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void restart(ActionEvent actionEvent) {
        if (login.userPermited(User.PERM_ADMIN)) {
            log = Console.request("iisreset /restart");
            FacesMessage msg = null;

            Pattern pattern = Pattern.compile("restarted");
            Matcher matcher = pattern.matcher(log);

            if (matcher.find()) {
                information = "Serwer IIS został zrestartowany pomyślnie!";
                msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Hura!", information);
            } else {
                information = "Błąd podczas restartowania serwera IIS! Spróbuj ponownie.";
                msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Błąd!", information);
            }
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public String getServerStatus() {
        //log = Console.request("iisreset /status");
        return null;
    }

    public String getLog() {
        return log;
    }

    public void setLog(String log) {
        this.log = log;
    }

    public String getInformation() {
        return information;
    }

    public void setInformation(String information) {
        this.information = information;
    }

    public Login getLogin() {
        return login;
    }

    public void setLogin(Login login) {
        this.login = login;
    }
    
}
